package com.finalproject.ui;
/**
 * VedningMachine tab panel
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.finalproject.entity.CardHolder;
import com.finalproject.entity.CardHolderDatabase;
import com.finalproject.entity.VendingMachine;
import com.finalproject.entity.VendingMachineDatabase;
import com.finalproject.entity.CardHolderDatabase.CardHolderDatabaseObserver;

public class UIVendingMachineTabPanel extends JPanel implements CardHolderDatabaseObserver {
	
	
	private UIVendingMachine uiVendingMachine;
	
	private UICardHolder uiCardHolder;
	
	private JPanel uiVendingMachinePanel;
	
	private UIMap uiMapPanel;
	
	
	private JPanel leftPanel;
	private JPanel rightPanel;
	private CardHolder cardHolder;
	
	private VendingMachineDatabase vendingMachineDatabase = VendingMachineDatabase.getInstance();
	
	public UIVendingMachineTabPanel() {
		CardHolderDatabase.getInstance().addCardHolderDatabaseObserver(this);
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createTitledBorder("VendingMachines"));
		this.uiVendingMachinePanel = new JPanel();
		leftPanel = new JPanel();
		
		rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		
		uiCardHolder = new UICardHolder();
		
		add(leftPanel, BorderLayout.WEST);
		add(rightPanel, BorderLayout.EAST);
		
		leftPanel.setLayout(new BorderLayout());
		
		leftPanel.add(uiVendingMachinePanel, BorderLayout.CENTER);
		
		
		
		this.uiMapPanel = createMap();
		uiMapPanel.setBorder(BorderFactory.createTitledBorder("Map Panel"));
		
		//uiMapPanel.setLayout(new FlowLayout());
		uiMapPanel.setPreferredSize(new Dimension(200,400));
		rightPanel.add(uiCardHolder,BorderLayout.NORTH);
		rightPanel.add(uiMapPanel,BorderLayout.CENTER);
		
		
		
		
		
		rightPanel.setPreferredSize(UIConstants.RIGHT_PANEL_PREFERRED);
		uiMapPanel.setLayout(null);
		
	}
	
	
	private UIMap createMap() {
		List<VendingMachine> vendingMachines = vendingMachineDatabase.getVendingMachines();		
		UIMap uiMap = new UIMap();
		if(!vendingMachines.isEmpty()) {
			VendingMachine vendingMachine = vendingMachines.get(0);
			showVendingMachine(vendingMachine);
		}
		
		
		for(int i = 0; i < vendingMachines.size(); i++) {
			final VendingMachine vendingMachine =  vendingMachines.get(i);
			String mapLabel = "V#" + vendingMachine.getId();
			int[] coordinates = vendingMachine.getCoordinates();
			JButton mapButton = new JButton(mapLabel);
			
			uiMap.addLocation(mapButton, coordinates);
			mapButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					showVendingMachine(vendingMachine);
				}
				
			});
		}
		return uiMap;
	}
	
	
	private void showVendingMachine(VendingMachine vendingMachine) {
		uiVendingMachinePanel.removeAll();
		uiVendingMachine = new UIVendingMachine(vendingMachine);
		uiVendingMachine.setCardHolder(cardHolder);
		uiVendingMachinePanel.add(uiVendingMachine);
		uiVendingMachinePanel.revalidate();
		
		
	}


	@Override
	public void onLogin(CardHolder cardHolder, Integer familyId) {
		this.cardHolder = cardHolder;
		uiVendingMachine.setCardHolder(cardHolder);
	}


	@Override
	public void onLogout(CardHolder cardHolder, Integer familyId) {
		// TODO Auto-generated method stub
		cardHolder = null;
		uiVendingMachine.setCardHolder(null);
	}

}
